import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export const store = new Vuex.Store({
  state: { //state存数据
    products: [{
        name: "马云",
        price: 200,
        id: 1
      },
      {
        name: "马化腾",
        price: 100,
        id: 14
      },
      {
        name: "马冬梅",
        price: 50,
        id: 11
      },
      {
        name: "马容",
        price: 20,
        id: 12
      }
    ]
  },
  getters: { //取数据
    saleProduts: (state) => {
      let saleProduts = state.products.map(product => {
        return {
          name: "**" + product.name + "**",
          price: product.price * 0.5,
          id: product.id
        };
      });
      return saleProduts;
    }
  },
  mutations: { //事件触发
    reducePrice: (state, payload) => {
      state.products.map(product => {
        // console.log(product.price)
        product.price -= payload;
      });
    }
  },
  actions: { //用于异步操作 传参
    reducePrice: (context, payload) => {
      setTimeout(() => {
        context.commit("reducePrice", payload)
      }, 2000)
    }

  }
})
// export default new Vuex.Store({
//   store
// });
